package br.com.fidalgotecnologia.layar.data;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.fidalgotecnologia.core.data.abs.BaseDAO;
import br.com.fidalgotecnologia.layar.entity.Layer;

public class LayerDAO extends BaseDAO<Layer> {
	private static LayerDAO instance;
	
	protected LayerDAO(){};
	
	public static LayerDAO getInstance() {
		if(instance == null){
			instance = new LayerDAO();
		}
		return instance;
	}

	@Override
	public Class<Layer> getClasse() {
		return Layer.class;
	}
	
	public Layer buscar(final Layer layer){
		Layer retorno = null;
		try {
			beginUserTransaction();
			EntityManager em = getEntityManager();
		    Session session = (Session)em.getDelegate();
		    session.beginTransaction();
		    Criteria criteria = session.createCriteria(getClasse());
		    criteria.add(Restrictions.eq("id", layer.getId()));
		    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		    retorno = (Layer)criteria.uniqueResult();
		    session.getTransaction().commit();
		    commitUserTransaction();
		} catch (Exception e) {
			retorno = null;
		}
	    return retorno;
	}

}
